﻿/*
// Exepack.NET compressor
// http://www.codeplex.com/exepack
//
// Console driver
// Written by Y [10-01-09]
// Copyright (c) 2008-2010 Alexey Yakovlev
//
// csc exepack.cs /res:loader.cs
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exepack
{
	class Program
	{
		static int Main(string[] args)
		{
			Console.WriteLine("Exepack.NET, version 0.03 beta{0}Copyright (c) 2008-2010 Alexey Yakovlev{0}", Environment.NewLine);

			if (args.Length != 1 || args[0].StartsWith("-") || args[0].IndexOf("?") >= 0)
			{
				Console.WriteLine("Syntax: exepack filename.exe");
				return 1;
			}

			using (var exepack = new ExeCompressor())
			{
				exepack.CompressionProgress += CompressionProgress;
				exepack.SavingExecutableProgress += SavingProgress;
				
				try
				{
					exepack.CompressExecutable(args[0]);
					return 0;
				}
				catch (Exception ex)
				{
					Console.WriteLine("{0}", ex.Message);
					return 1;
				}
			}
		}

		static bool first = true;

		static void CompressionProgress(object sender, CompressionProgressEventArgs args)
		{
			if (args.Completed)
				return;

			if (first)
			{
				first = false;
				Console.WriteLine("Compressing executable: {0}", args.FileName);
				Console.WriteLine("Compressing additional assemblies:");
				return;
			}

			Console.WriteLine("\t{0}", args.FileName);
		}

		static void SavingProgress(object sender, CompressionProgressEventArgs args)
		{
			if (!args.Completed)
			{
				Console.WriteLine("Saving compressed executable file: {0}", args.FileName);
				return;
			}

			Console.WriteLine("Compression: {0} -> {1} bytes ({2})", args.OriginalSize, args.CompressedSize,
				((decimal)args.CompressedSize / args.OriginalSize).ToString("#0.##%"));
		}
	}
}
